#include "Weapon.h"
#include "Barrage.h"
#include "STGEngine.h"

#include <tolua++.h>
#include <lua.hpp>
#include <lualib.h>
#include <lauxlib.h>
#include "Defs.h"
CWeapon::CWeapon(const std::string &name, const std::string &scriptName)
: GameObject(name, scriptName)
, mPower(100)
, mQuantity(100)
, mSpeed(100)
{
    //mBarrage = CBarrage::createBarrage(name);
}


GameInst* CWeapon::createInstance(const std::string &name) {
    GameInst *pInst = new CWeaponInst(this, name);
    mInstances.push_back(pInst);
    return pInst;
}
void CWeapon::loadConfigScript()
{
    lua_State *L = static_cast<CSTGEngine*>(CSTGEngine::getSingletonPtr())->lua();
    if (luaL_dofile(L, mScriptName.c_str())) 
        lua_error(L);

    GET_LUA_INT(L, "quantity", mQuantity);
    GET_LUA_FLOAT(L, "power", mPower);
    GET_LUA_FLOAT(L, "speed", mSpeed);
    GET_LUA_STRING(L, "barrage", mBarrageName);
    //lua_getglobal(L, "speed");
    //if (lua_isnumber(L, -1)) {
    //    mSpeed = lua_tointeger(L,-1);
    //}
}

//==============================================================================
//
CWeaponInst::CWeaponInst(CWeapon *pObject, const std::string &name)
: GameInst(pObject, name) 
, mCurQuantity(100)
, mBarrage(NULL)
{
    mBarrage = static_cast<CSTGEngine *> (CSTGEngine::getSingletonPtr())->
        createBarrage(pObject->getBarrageName());
    //mNode = mBarrage->
}

void CWeaponInst::update(float dt) {
    if (mBarrage) {
        mBarrage->setParentPos(mNode->_getDerivedPosition());
        mBarrage->_update(dt);
    }
}
